//解码方法
//测试链接：https://leetcode.cn/problems/decode-ways/
public class NumDecodings {
    public int numDecodings(String s) {
        char[] arr = s.toCharArray();
        int[] dp = new int[arr.length];  //dp[i]表示以i位置结尾存在的编码个数

        //初始化
        if(arr[0] != '0') dp[0] = 1;
        if(arr.length == 1) return dp[0];

        if(arr[0] != '0' && arr[1] != '0') dp[1] += 1;
        int num = (arr[0]-'0') * 10 + (arr[1] - '0');
        if(num >= 10 && num <= 26) dp[1] += 1;

        for(int i = 2; i < arr.length; i++) {
            if(arr[i] != '0') dp[i] = dp[i-1];

            int temp = (arr[i-1]-'0') * 10 + (arr[i] - '0');
            if(temp >= 10 && temp <= 26) dp[i] += dp[i-2];

        }
        return dp[arr.length-1];
    }
}
